package com.fhj.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhj.bookshop.entity.Customer;
import com.fhj.bookshop.mapper.CustomerMapper;
import com.fhj.bookshop.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-12-12
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Override
    public Customer getByAccountAndPassword(String account,String password){
        LambdaQueryWrapper<Customer> wrapper=new LambdaQueryWrapper<Customer>();
        wrapper.eq(Customer::getCustAccount,account);
        wrapper.eq(Customer::getCustPwd,password);
        return customerMapper.selectOne(wrapper);
    }

    @Override
    public Customer getByAccount(String account) {
        LambdaQueryWrapper<Customer> wrapper=new LambdaQueryWrapper<Customer>();
        wrapper.eq(Customer::getCustAccount,account);
        return customerMapper.selectOne(wrapper);
    }

    @Override
    public void addUser(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public Customer getById(Integer custId) {
        LambdaQueryWrapper<Customer> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Customer::getCustId,custId);
        return customerMapper.selectOne(wrapper);
    }

    @Override
    public Page<Customer> otherUserPage(Integer custId, Integer pageCode, Integer pageSize){
        Page<Customer> page =new Page<Customer>(pageCode,pageSize);
        LambdaQueryWrapper<Customer> wrapper=new LambdaQueryWrapper<>();
        wrapper.ne(Customer::getCustId,custId);
        Page<Customer> pageInfo=customerMapper.selectPage(page,wrapper);
        return pageInfo;
    }

    @Override
    public void changePower(Integer custId, String power){
        Customer customer=customerMapper.selectById(custId);
        customer.setStatus(power);
        customerMapper.updateById(customer);
    }
}
